package com.frame.util;

import java.text.*;
import java.util.*;

/**
 * <p>Title: 日期时间转化</p>
 * <p>Description: </p>
 * <p>Copyright: Copyright (c) 2006</p>
 * <p>Company: </p>
 * @author ccs
 * @version 1.0
 */

public final class DateConvert
{
   /**
    * 日期时间格式
    */
   static private List formats = new ArrayList();

   /**
    * 默认的日期时间格式
    */
   static private SimpleDateFormat defaultFormat;

   static private SimpleDateFormat defaultDateFormat;

   static private SimpleDateFormat defaultTimeFormat;

   static private SimpleDateFormat defaultTimestampFormat;

   static private final SimpleDateFormat FORMAT = new SimpleDateFormat(
           "yyyy-MM-dd HH:mm:ss");

   static private long l = 1000 * 60 * 60 * 24;

   static private Map formatMap = new HashMap();

   static
   {
      //默认添加国际通用格式

      addFormat("yyyy-MM-dd HH:mm:ss");
      addFormat("yyyy-MM-dd HH:mm");
      addFormat("yyyy-MM-dd");
      addFormat("MM/dd/yyyy");
      addFormat("HH:mm:ss");
      addFormat("HH:mm");
      addFormat("MM/dd/yyyy HH:mm:ss");
      addFormat("MM/dd/yyyy HH:mm");
      addFormat("yyyy年MM月dd日");
      addFormat("yyyy年MM月dd日 HH:mm:ss");
      addFormat("yyyy年MM月dd日 HH:mm");
      setDefaultFormat("yyyy-MM-dd HH:mm:ss");
      setDefaultDateFormat("yyyy-MM-dd");
      setDefaultTimeFormat("HH:mm:ss");
      setDefaultTimestampFormat("yyyy-MM-dd HH:mm:ss");
   }

   private DateConvert()
   {
   }

   static public void addFormat(String format)
   {
      int n = formats.size();
      for (int i = 0; i < n; i++)
      {
         SimpleDateFormat dateFormat = (SimpleDateFormat) formats.get(i);
         if (dateFormat.toPattern().equals(format))
            return;
      }

      formats.add(new SimpleDateFormat(format));
   }

   static public void setDefaultFormat(String format)
   {
      int n = formats.size();
      for (int i = 0; i < n; i++)
      {
         SimpleDateFormat dateFormat = (SimpleDateFormat) formats.get(i);
         if (dateFormat.toPattern().equals(format))
         {
            defaultFormat = dateFormat;
            return;
         }
      }

      SimpleDateFormat dateFormat = new SimpleDateFormat(format);
      formats.add(dateFormat);
      defaultFormat = dateFormat;
   }

   static public void setDefaultDateFormat(String format)
   {
      int n = formats.size();
      for (int i = 0; i < n; i++)
      {
         SimpleDateFormat dateFormat = (SimpleDateFormat) formats.get(i);
         if (dateFormat.toPattern().equals(format))
         {
            defaultDateFormat = dateFormat;
            return;
         }
      }

      SimpleDateFormat dateFormat = new SimpleDateFormat(format);
      formats.add(dateFormat);
      defaultDateFormat = dateFormat;
   }

   static public void setDefaultTimeFormat(String format)
   {
      int n = formats.size();
      for (int i = 0; i < n; i++)
      {
         SimpleDateFormat dateFormat = (SimpleDateFormat) formats.get(i);
         if (dateFormat.toPattern().equals(format))
         {
            defaultTimeFormat = dateFormat;
            return;
         }
      }

      SimpleDateFormat dateFormat = new SimpleDateFormat(format);
      formats.add(dateFormat);
      defaultTimeFormat = dateFormat;
   }

   static public void setDefaultTimestampFormat(String format)
   {
      int n = formats.size();
      for (int i = 0; i < n; i++)
      {
         SimpleDateFormat dateFormat = (SimpleDateFormat) formats.get(i);
         if (dateFormat.toPattern().equals(format))
         {
            defaultTimestampFormat = dateFormat;
            return;
         }
      }

      SimpleDateFormat dateFormat = new SimpleDateFormat(format);
      formats.add(dateFormat);
      defaultTimestampFormat = dateFormat;
   }

   /**
    * 将时间日期转化为字符串
    * @param date
    * @return
    */
   static public String toString(Date date)
   {
      if (date instanceof java.sql.Date)
         return defaultDateFormat.format(date);
      else if (date instanceof java.sql.Time)
         return defaultTimeFormat.format(date);
      else if (date instanceof java.sql.Timestamp)
         return defaultTimestampFormat.format(date);

      return defaultFormat.format(date);
   }

   static public String toString(Date date, String format)
   {
      DateFormat dateFormat = null;
      synchronized (formatMap)
      {
         dateFormat = (DateFormat) formatMap.get(format);
         if (dateFormat == null)
         {
            dateFormat = new SimpleDateFormat(format);
            formatMap.put(format, dateFormat);
         }
      }

      return dateFormat.format(date);
   }
   static public String objToString(Object obj, String format)
   {
      Date date=null;
      if(obj instanceof java.sql.Timestamp){
         date=new Date(((java.sql.Timestamp)obj).getTime());
      }else
         date=(Date)obj;
      if(format!=null && !"".equals(format))
         return toString(date,format);
      else
         return toString(date);
   }
   static public String toDefaultString(Date date)
   {
      return FORMAT.format(date);
   }

   /**
    * 将字符串转化为时间日期
    * @param s
    * @return
    */
   static public Date toDate(String s)
   {
      int n = formats.size();
      Date date=null;
      for (int i = 0; i < n; i++)
      {
         SimpleDateFormat format = (SimpleDateFormat) formats.get(i);
         try
         {
            date= format.parse(s);
            if(date!=null)
               return date;
         }
         catch (ParseException ex)
         {
            //一个格式不符合，尝试下一个格式
         }
      }
      if(date==null)
         System.out.println("时间转换过程发生错误！:"+s+"时间格式不正确");
      return null;
   }

   /**
    * 获得季度
    * @param date
    * @return
    */
   static public int getQuarter(Date date)
   {
      return date.getMonth() / 3;
   }

   /**
    * 对一个日期取整
    * @param date
    * @return
    */
   static public Date truncate(Date date)
   {
      long l = 1000 * 60 * 60 * 24;
      return new Date( (date.getTime() + 1000 * 60 * 60 * 8) / l * l -
              1000 * 60 * 60 * 8);
   }

   /**
    * 增加天
    * @param date
    * @param day
    * @return
    */
   static public Date addDate(Date date, int day)
   {
      return new Date(date.getTime() + day * l);
   }

   /**
    * 增加月
    * @param date
    * @param month
    * @return
    */
   static public Date addMonth(Date date, int month)
   {
      Calendar c = Calendar.getInstance();
      c.setTime(date);
      c.add(Calendar.MONTH, month);

      return c.getTime();
   }

   /**
    * 增加年
    * @param date
    * @param year
    * @return
    */
   static public Date addYear(Date date, int year)
   {
      date = new Date(date.getTime());
      date.setYear(date.getYear() + year);

      return date;
   }

   /**
    * 增加星期
    * @param date
    * @param week
    * @return
    */
   static public Date addWeek(Date date, int week)
   {
      return addDate(date, week * 7);
   }

   /**
    * 增加季度
    * @param date
    * @param quarter
    * @return
    */
   static public Date addQuarter(Date date, int quarter)
   {
      return addMonth(date, quarter * 3);
   }

   /**
    * 获得一周开始
    * @param date
    * @return
    */
   static public Date getWeekStart(Date date)
   {
      date = truncate(date);
      date = addDate(date, -date.getDay());
      return date;
   }

   /**
    * 获得一周结束
    * @param date
    * @return
    */
   static public Date getWeekEnd(Date date)
   {
      date = truncate(date);
      date = addDate(date, 7 - date.getDay());
      return date;
   }

   /**
    * 获得当月的开始
    * @param date
    * @return
    */
   static public Date getMonthStart(Date date)
   {
      date = truncate(date);
      date.setDate(1);

      return date;
   }

   /**
    * 获得当月的结束
    * @param date
    * @return
    */
   static public Date getMonthEnd(Date date)
   {
      return addMonth(getMonthStart(date), 1);
   }

   /**
    * 获得季度的开始
    * @param date
    * @return
    */
   static public Date getQuarterStart(Date date)
   {
      date = getMonthStart(date);
      int month = date.getMonth();
      date.setMonth(month - (month % 3) );

      return date;
   }

   /**
    * 获得季度的结束
    * @param date
    * @return
    */
   static public Date getQuerterEnd(Date date)
   {
      date = getMonthStart(date);
      int month = date.getMonth();
      date.setMonth(month + 3 - month % 3);

      return date;
   }

   /**
    * 获得一年的开始
    * @param date
    * @return
    */
   static public Date getYearStart(Date date)
   {
      date = getMonthStart(date);
      date.setMonth(0);

      return date;
   }

   /**
    * 获得一年的结束
    * @param date
    * @return
    */
   static public Date getYearEnd(Date date)
   {
      return addYear(getYearStart(date), 1);
   }

   static public Date getYearStart(int year)
   {
      return new Date(year - 1900, 0, 1);
   }

   static public Date getYearEnd(int year)
   {
      return new Date(year - 1899, 0, 1);
   }

   /**
    * 获得半年开始
    * @param date
    * @return
    */
   static public Date getHalfYearStart(Date date)
   {
      date = getMonthStart(date);
      int month = date.getMonth();
      if (month < 6)
         date.setMonth(0);
      else
         date.setMonth(6);

      return date;
   }

   /**
    * 获得半年结束
    * @param date
    * @return
    */
   static public Date getHalfYearEnd(Date date)
   {
      date = getMonthStart(date);
      int month = date.getMonth();
      if (month < 6)
      {
         date.setMonth(6);
      }
      else
      {
         date.setMonth(0);
         date.setYear(date.getYear() + 1);
      }

      return date;
   }
}